package com.ganzi.ganziserver.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ganzi.ganziserver.core.CollectInformation;
import com.ganzi.ganziserver.core.ControlLog;
import com.ganzi.ganziserver.core.TempCollect;
import com.ganzi.ganziserver.core.User;
import com.ganzi.ganziserver.service.CollectService;
import com.ganzi.ganziserver.service.ControlLogService;
import com.ganzi.ganziserver.shiro.redis.OtherCache.UserCache;
import com.ganzi.ganziserver.utils.GetTime;
import com.ganzi.ganziserver.utils.PageBean;
import com.ganzi.ganziserver.utils.ReturnMap;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;



@RestController
@RequestMapping("/info")
public class OtherController {

    @Autowired
    CollectService collectService;
    @Autowired
    ControlLogService controlLogService;
    @Autowired
    UserCache cache;

    @RequestMapping(value ="/fanCollectInformation",method = {RequestMethod.POST})
    public Object fanCollectInformation(@RequestBody String json){
        try{
            JSONObject jsons = (JSONObject) JSON.parse(json);
            long schoolId = jsons.getLongValue("schoolId");
            if(schoolId != 0){
                List<TempCollect> temp = collectService.getCollectBySchool(schoolId);
                return ReturnMap.getReturnMapData(200, "请求成功",temp);
            }
            return ReturnMap.getReturnMap(200, "请求成功");
        }catch (Exception e){
            return ReturnMap.getReturnMap(400, "服务器异常");
        }
    }

    @RequiresPermissions("findlog")
    @RequestMapping(value ="/controlInformation",method = {RequestMethod.POST})
    public Object getControlLog(@RequestBody String json){
        try{
            JSONObject jsons = null;
            if(StringUtils.isNotBlank(json))jsons= (JSONObject) JSON.parse(json);
            long schoolId = 0;
            int sendState =0;
            int currentPage = 1;
            int pageSize = 100;
            long beforeTime;
            long afterTime;
            if(jsons != null && jsons.containsKey("schoolId")){
                schoolId = jsons.getLongValue("schoolId");
            }
            if(jsons != null && jsons.containsKey("sendState")){
                sendState = jsons.getIntValue("sendState");
            }
            if(jsons != null && jsons.containsKey("beforeTime")){
                beforeTime = jsons.getTimestamp("beforeTime").getTime();
            }else{
                beforeTime = GetTime.getNowTime().getTime();
            }
            if(jsons != null && jsons.containsKey("afterTime")){
                afterTime = jsons.getTimestamp("afterTime").getTime();
            }else{
                afterTime = GetTime.getBeforMonthNowTime().getTime();
            }
            if(jsons != null && jsons.containsKey("currentPage")){
                currentPage = jsons.getIntValue("currentPage");
            }
            if(jsons != null && jsons.containsKey("pageSize")){
                pageSize = jsons.getIntValue("pageSize");
            }
            User tempUser = cache.getUserByPrincipal();
            long uid = tempUser.getId();
            PageHelper.startPage(currentPage, pageSize);
            List<ControlLog> list = controlLogService.findLog(uid, schoolId, sendState, beforeTime, afterTime);
            int count = controlLogService.findLogCount(uid, schoolId, sendState, beforeTime, afterTime);
            PageBean<ControlLog> pageData = new PageBean<>(currentPage, pageSize, count);
            pageData.setItems(list);
            return ReturnMap.getReturnMapDataForPage(200, "请求成功",pageData.getItems(),pageData.getTotalPage(),pageData.getCurrentPage());
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMap.getReturnMap(400, "服务器异常");
        }
    }

    @RequiresPermissions("findhistory")
    @RequestMapping(value ="/findFanHistory",method = {RequestMethod.POST})
    public Object findFanHistory(@RequestBody String json){
        try{
            JSONObject jsons = null;
            if(StringUtils.isNotBlank(json))jsons= (JSONObject) JSON.parse(json);
            long fanId = 0l;
            long beforeTime;
            int size = 100;
            if(jsons != null && jsons.containsKey("fanId")){
                fanId = jsons.getLongValue("fanId");
            }
            if(jsons != null && jsons.containsKey("size")){
                size = jsons.getIntValue("size");
            }
            if(jsons != null && jsons.containsKey("beforeTime")){
                beforeTime = jsons.getTimestamp("beforeTime").getTime();
            }else{
                beforeTime = GetTime.getNowTime().getTime();
            }
            List<CollectInformation> temp = null;
            if(fanId != 0l){
                temp = controlLogService.findCollectInformation(fanId,beforeTime,size);
            }
            return ReturnMap.getReturnMapData(200, "请求成功",temp);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMap.getReturnMap(400, "服务器异常");
        }
    }
}
